Registry
Patterns and utilities for handling registration of metadata and functionality for run-time use
Install: @travetto/registry
npm install @travetto/registry
This module is the backbone for all "discovered" and "registered" behaviors within the framework. This is primarily used for building modules within the framework and not directly useful for application development.
Flows
Registration, within the framework flows throw two main use cases:
Initial Flows
The primary flow occurs on initialization of the application. At that point, the module will:
- Initialize RootRegistry and will automatically register/load all relevant files
- As files are imported, decorators within the files will record various metadata relevant to the respective registries
- When all files are processed, the RootRegistry is finished, and it will signal to anything waiting on registered data that its free to use it.
This flow ensures all files are loaded and processed before application starts. A sample registry could like:
Code: Sample Registry
import { Class } from '@travetto/base';
import { MetadataRegistry } from '@travetto/registry';
interface Group {
class: Class;
name: string;
}
interface Child {
name: string;
method: Function;
}
export class SampleRegistry extends MetadataRegistry<Group, Child> {
onInstallFinalize<T>(cls: Class<T>): Group {
return this.getOrCreatePending(cls) as Group;
}
createPending(cls: Class): Partial<Group> {
return {
class: cls,
name: cls.name
};
}
}
The registry is a MetadataRegistry that similar to the SchemaRegistry and the DependencyRegistry.
Live Flow
At runtime, the registry is designed to listen for changes and to propagate the changes as necessary. In many cases the same file is handled by multiple registries.
As the Compiler notifies that a file has been changed and recompiled, the RootRegistry will pick it up, and process it accordingly.
Supporting Metadata
For the registries to work properly, metadata needs to be collected about files and classes to uniquely identify them, especially across file reloads for the live flow. To achieve this, every class
is decorated with additional fields. The data that is added is:
ᚕfile
denotes the fully qualified path name of the classᚕid
represents a computed id that is tied to the file/class combination;